package com.project.gateway.filter;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 全局过滤器
 */
@Slf4j
@Component
public class GlobalGatewayFilter implements GlobalFilter {
    private final static AntPathMatcher matcher = new AntPathMatcher();

    @SneakyThrows
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String path = request.getURI().getPath();
        String method = request.getMethodValue();
        log.info("请求URL：{}:{}", method, path);
        if (request.getMethod().equals(HttpMethod.POST) || request.getMethod().equals(HttpMethod.PUT)) {
            if (matcher.match("/auth/**", path)) {
                return chain.filter(exchange);
            } else if (matcher.match("/api/pushtask/**", path)) {
                return chain.filter(exchange);
            } else if (matcher.match("/api/datacenter/**", path)) {
                return chain.filter(exchange);
            } else if (matcher.match("/api/pudongdata/**", path)) {
                return chain.filter(exchange);
            } else if (matcher.match("/api/workjob/**", path)) {
                return chain.filter(exchange);
            } else if (matcher.match("/api/webpage/**", path)) {
                if(!path.contains("screen")) {
                    HttpHeaders headers = request.getHeaders();
                    String user = headers.getFirst("user");
                    if (StringUtils.isEmpty(user)) {
                        log.error("未授权或登录已过期");
                        throw new Exception("未授权或登录已过期");
                    }
                }
                return chain.filter(exchange);
            } else if (matcher.match("/api/rfidpage/**", path)) {
                HttpHeaders headers = request.getHeaders();
                String user = headers.getFirst("user");
                if (StringUtils.isEmpty(user)) {
                    log.error("未授权或登录已过期");
                    throw new Exception("未授权或登录已过期");
                }
                return chain.filter(exchange);
            } else {
                log.error("非正常请求");
                throw new Exception("非正常请求");
            }
        } else if (request.getMethod().equals(HttpMethod.GET)) {
            if (matcher.match("/api/webpage/file/**", path)) {
                return chain.filter(exchange);
            } else {
                log.error("非正常请求");
                throw new Exception("非正常请求");
            }
        } else {
            log.error("非正常请求");
            throw new Exception("非正常请求");
        }
    }
}
